/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.core.dao.Day_offDAO;
import com.core.database.Manager;
import com.core.entities.Day_off;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class Day_offDAOImpl implements Day_offDAO {
		Day_off day_off = new Day_off();
		ArrayList<Day_off> day_offList = new ArrayList<Day_off>();

	Connection connection = null;
	private static Day_offDAOImpl instance=null;
	public static Day_offDAOImpl getInstance(){
		if(instance==null){
			instance=new Day_offDAOImpl();
		}
		return instance;
	}

	@Override
	public Day_off findByDate(Date date) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT *FROM day_off WHERE name = '" + date + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					day_off.setId(rs.getInt(1));
					day_off.setDate(rs.getDate(2));
					day_off.setDay(rs.getString(3));
					day_off.setReason(rs.getString(4));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return day_off;
	}

	@Override
	public Day_off findByDay(String day) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT *FROM day_off WHERE name = '" + day + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					day_off.setId(rs.getInt(1));
					day_off.setDate(rs.getDate(2));
					day_off.setDay(rs.getString(3));
					day_off.setReason(rs.getString(4));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return day_off;
	}

	@Override
	public Day_off findByReason(String reason) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT *FROM day_off WHERE name = '" + reason + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					day_off.setId(rs.getInt(1));
					day_off.setDate(rs.getDate(2));
					day_off.setDay(rs.getString(3));
					day_off.setReason(rs.getString(4));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return day_off;
	}

	@Override
	public void insert(Day_off o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO day_off (date, day, reason) VAULE (?, ?, ?)";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setDate(1, o.getDate());
			ps.setString(2, o.getDay());
			ps.setString(3, o.getReason());
			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new Day_off", ex);
		} finally {
			Manager.closeConnection(connection);

		}
	}

	@Override
	public Day_off findById(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT *FROM day_offWHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					day_off.setId(rs.getInt(1));
					day_off.setDate(rs.getDate(2));
					day_off.setDay(rs.getString(3));
					day_off.setReason(rs.getString(4));
				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return day_off;
	}

	@Override
	public List<Day_off> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM day_off ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				day_off.setId(rs.getInt(1));
				day_off.setDate(rs.getDate(2));
				day_off.setDay(rs.getString(3));

				day_offList.add(day_off);

				System.out.println(day_off);
			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return day_offList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM day_off WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setDate(1, day_off.getDate());
				ps.setString(2, day_off.getDay());
				ps.setString(3, day_off.getReason());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(Day_off o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "UPDATE day_off SET date = '" + o.getDate()
					+ " ', day = '" + o.getDay() + "', reason = '"
					+ o.getReason() + "' WHERE id = " + o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setDate(1, o.getDate());
				ps.setString(2, o.getDay());
				ps.setString(3, o.getReason());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
